home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Codigo / Un ejercicio de presentación de texto / SysInfoReflection / SysInfoReflectionStrings.cs < prev   
Encoding:
Text File  |  2002-05-16  |  3.7 KB  |  122 lines

  1. //-------------------------------------------------------
  2. // SysInfoReflectionStrings.cs ⌐ 2001 by Charles Petzold
  3. //-------------------------------------------------------
  4. using Microsoft.Win32;
  5. using System;
  6. using System.Drawing;
  7. using System.Reflection;
  8. using System.Windows.Forms;
  9.  
  10. class SysInfoReflectionStrings
  11. {
  12.                                                             // Campos
  13.      static bool     bValidInfo = false;
  14.      static int      iCount;
  15.      static string[] astrLabels;
  16.      static string[] astrValues;
  17.                                                             // Constructor
  18.      static SysInfoReflectionStrings()
  19.      {
  20.           SystemEvents.UserPreferenceChanged += 
  21.                new UserPreferenceChangedEventHandler(UserPreferenceChanged);
  22.  
  23.           SystemEvents.DisplaySettingsChanged +=
  24.                new EventHandler(DisplaySettingsChanged);
  25.      }
  26.                                                             // Propiedades
  27.      public static string[] Labels
  28.      {
  29.           get
  30.           {
  31.                GetSysInfo();
  32.                return astrLabels;
  33.           }
  34.      }
  35.      public static string[] Values
  36.      {
  37.           get
  38.           {
  39.                GetSysInfo();
  40.                return astrValues;
  41.           }
  42.      }
  43.      public static int Count
  44.      {
  45.           get 
  46.           { 
  47.                GetSysInfo();
  48.                return iCount;
  49.           }
  50.      }
  51.                                                             // Controladores de Eventos
  52.      static void UserPreferenceChanged(object obj, 
  53.                                        UserPreferenceChangedEventArgs ea)
  54.      {
  55.           bValidInfo = false;
  56.      }
  57.      static void DisplaySettingsChanged(object obj, EventArgs ea)
  58.      {
  59.           bValidInfo = false;
  60.      }
  61.                                                             // MΘtodos
  62.      static void GetSysInfo()
  63.      {
  64.           if(bValidInfo)
  65.                return;
  66.  
  67.                // Obtenci≤n de la informaci≤n de las propiedades de la clase SystemInformation.
  68.  
  69.           Type type = typeof(SystemInformation);
  70.           PropertyInfo[] apropinfo = type.GetProperties();
  71.  
  72.                // Recuento del n·mero de propiedades estßticas.
  73.  
  74.           iCount = 0;
  75.           foreach (PropertyInfo pi in apropinfo)
  76.           {
  77.                if(pi.CanRead && pi.GetGetMethod().IsStatic)
  78.                     iCount++;
  79.           }
  80.                // Asignaci≤n de las matrices de cadenas
  81.  
  82.           astrLabels = new string[iCount];
  83.           astrValues = new string[iCount];
  84.  
  85.                // De nuevo bucle sobre la informaci≤n de las propiedades de las clases.
  86.  
  87.           iCount = 0;
  88.           foreach (PropertyInfo pi in apropinfo)
  89.           {
  90.                if(pi.CanRead && pi.GetGetMethod().IsStatic)
  91.                {
  92.                          // Obtenci≤n de los nombres y los valores de las propiedades.
  93.  
  94.                     astrLabels[iCount] = pi.Name;
  95.                     astrValues[iCount] = pi.GetValue(type, null).ToString();
  96.                     iCount++;
  97.                }
  98.           }
  99.           Array.Sort(astrLabels, astrValues);
  100.           bValidInfo = true;
  101.      }
  102.      public static float MaxLabelWidth(Graphics grfx, Font font)
  103.      {
  104.           return MaxWidth(Labels, grfx, font);
  105.      }
  106.      public static float MaxValueWidth(Graphics grfx, Font font)
  107.      {
  108.           return MaxWidth(Values, grfx, font);
  109.      }
  110.      static float MaxWidth(string[] astr, Graphics grfx, Font font) 
  111.      {
  112.           float fMax = 0;
  113.  
  114.           GetSysInfo();
  115.  
  116.           foreach (string str in astr)
  117.                fMax = Math.Max(fMax, grfx.MeasureString(str, font).Width);
  118.  
  119.           return fMax;
  120.      }
  121. }
  122.